package com.ruoyi.security.controller;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.security.domain.AfElevatorRepair;
import com.ruoyi.security.domain.ElevatorInfoList;
import com.ruoyi.security.domain.ElevatorResult;
import com.ruoyi.security.elevator.util.ElevatorInterfaceUtil;
import com.ruoyi.security.service.IAfElevatorRepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 电梯维修单Controller
 *
 * @author cjie
 * @date 2022-07-25
 */
@Api(tags = " 电梯维修单")
@RestController
@RequestMapping("/afElevatorRepair")
public class AfElevatorRepairController extends BaseController {
    @Autowired
    private IAfElevatorRepairService afElevatorRepairService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询电梯维修单列表
     */
    @ApiOperation(value="查询电梯维修单列表", notes="查询电梯维修单列表")
    @PreAuthorize(hasPermi = "security:afElevatorRepair:list")
    @GetMapping("/list")
    public TableDataInfo list(AfElevatorRepair afElevatorRepair) {
        startPage();
        List<AfElevatorRepair> list = afElevatorRepairService.selectAfElevatorRepairList(afElevatorRepair);
        return getDataTable(list);
    }

    /**
     * 导出电梯维修单列表
     */
    @ApiOperation(value="导出电梯维修单列表", notes="导出电梯维修单列表")
    @PreAuthorize(hasPermi = "security:afElevatorRepair:export")
    @Log(title = "电梯维修单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AfElevatorRepair afElevatorRepair) throws IOException {
        List<AfElevatorRepair> list = afElevatorRepairService.selectAfElevatorRepairList(afElevatorRepair);
        ExcelUtil<AfElevatorRepair> util = new ExcelUtil<AfElevatorRepair>(AfElevatorRepair.class);
        util.exportExcel(response, list, "电梯维修单数据");
    }

    /**
     * 获取电梯维修单详细信息
     */
    @ApiOperation(value="获取电梯维修单详细信息", notes="获取电梯维修单详细信息")
    @PreAuthorize(hasPermi = "security:afElevatorRepair:query")
    @GetMapping(value = "/{orderNo}")
    public AjaxResult getInfo(@PathVariable("orderNo") String orderNo) {
        return AjaxResult.success(afElevatorRepairService.selectAfElevatorRepairById(orderNo));
    }

    /**
     * 新增电梯维修单
     */
    @ApiOperation(value="新增电梯维修单", notes="新增电梯维修单")
    @PreAuthorize(hasPermi = "security:afElevatorRepair:add")
    @Log(title = "电梯维修单", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@RequestBody AfElevatorRepair afElevatorRepair) {
        return toAjax(afElevatorRepairService.insertAfElevatorRepair(afElevatorRepair));
    }

    /**
     * 修改电梯维修单
     */
    @ApiOperation(value="修改电梯维修单", notes="修改电梯维修单")
    @PreAuthorize(hasPermi = "security:afElevatorRepair:edit")
    @Log(title = "电梯维修单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AfElevatorRepair afElevatorRepair) {
        return toAjax(afElevatorRepairService.updateAfElevatorRepair(afElevatorRepair));
    }

    /**
     * 删除电梯维修单
     */
    @ApiOperation(value="删除电梯维修单", notes="删除电梯维修单")
    @ApiImplicitParam(name = "ids", value = "电梯维修单ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "security:afElevatorRepair:remove")
    @Log(title = "电梯维修单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderNos}")
    public AjaxResult remove(@PathVariable String[] orderNos) {
        return toAjax(afElevatorRepairService.deleteAfElevatorRepairByIds(orderNos));
    }

    @ApiOperation(value="电梯维修单同步", notes="电梯维修单同步")
    @PreAuthorize(hasPermi = "security:afElevatorRepair:sync")
    @GetMapping("/syncInfo")
    public AjaxResult syncInfo() {

        String token = redisService.getCacheObject("elevator_token");
        if (StringUtils.isBlank(token)){
            return AjaxResult.error("梯联网token为空");
        }
        for (int page = 1;; page ++){
            ElevatorResult<ElevatorInfoList> result = ElevatorInterfaceUtil.getRepairList(token,page +"", 50 +"",null ,null,null);
            if ("0".equals(result.getErrorCode())){
                List<AfElevatorRepair> list = result.getData().getRows();
                //插入或者更新
                list.forEach(o ->{
                    afElevatorRepairService.insertAfElevatorRepair(o);
                });
                if (list.size() <50){
                    return AjaxResult.success();
                }
            }else {
                logger.error("error ==>" + result.getErrorMsg());
                return AjaxResult.error(result.getErrorMsg());
            }

        }

    }

}
